package cn.labbook.bluetooth.controller;

import cn.labbook.bluetooth.common.ResultDto;
import cn.labbook.bluetooth.common.TokenManager;
import cn.labbook.bluetooth.service.IAdminService;
import cn.labbook.bluetooth.shiro.SysToken;
import cn.labbook.bluetooth.util.IPUtils;
import cn.labbook.bluetooth.util.MD5Utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
@Controller
public class AuthController extends AbstractController {
    @RequestMapping("/auth/loginSubmit")
    @ResponseBody
    public ResultDto loginSubmit(@RequestParam String username, @RequestParam String password,
                                 HttpServletRequest request) {

        SysToken sysToken = new SysToken(username, MD5Utils.string2MD5(password), IPUtils.getRealIp(request));
        Subject subject = SecurityUtils.getSubject();
        subject.login(sysToken);
        if (subject.isAuthenticated()) {
            TokenManager.getSession().setAttribute("admin",TokenManager.getToken().getAdmin());
            return success();
        }
        return fail();
    }
}
